package echecs.init;

import echecs.vue.BoardVue;
import echecs.vue.BoardVue;
import java.applet.Applet;

public abstract class Run extends BoardVue {

    public Run(Applet ref) {
        super(ref);
    }
    public static int color = 1;	//color of the player that can move
    Thread th = null;	//AI thread
    public static int movecounter = 0;
    public static int deep = 0;	//actual deep
    public static int target = 4;	//target deep
    public static float value = 0;	//minimax

    public void run() {
        //no access to the movelist
        code = 1;

        deep = 0;
        target = 4;

        //look for best move
        movecounter = 0;
        genmove();

        if (movecounter == 0) //no moves -> end of game
        {
            if (ischeck()) {
                parent.getAppletContext().showStatus("white wins!");
            } else {
                parent.getAppletContext().showStatus("game is a draw!");
            }
            return;
        }
        //execute move
        execute(move / 100, move % 100);

        //give accesss to the movelist
        code = 0;
    }

    public void execute(int start, int end) {
        board[end] = board[start];
        board[start] = 0;
        //Rochade ?
        if (board[end] % 10 == 6) {
            if (end == start + 2) {
                //little
                board[start + 1] = board[start + 3] % 100;
                board[start + 3] = 0;
                graphboard[start + 1] = board[start + 1];
                graphboard[start + 3] = 0;
                paintField(start + 3);
                paintField(start + 1);
            }
            if (end == start - 2) {
                //big
                board[start - 1] = board[start - 4] % 100;
                board[start - 4] = 0;
                graphboard[start - 1] = board[start - 1];
                graphboard[start - 4] = 0;
                paintField(start - 4);
                paintField(start - 1);
            }
        }
        //change pawn?
        if ((board[end] % 10 == 1) && ((end < 29) || (end > 90))) {
            board[end] += 4;
        }
        graphboard[start] = board[start];
        graphboard[end] = board[end];
        paintField(end);
        paintField(start);
        //change player
        if (color == 1) {
            color = 2;
            th = new Thread(this);
            th.setPriority(10);
            th.start();
        } else {
            color = 1;
            //calculate valid moves
            movecounter = 0;
            deep = 0;
            target = 1;
            genmove();
            if (movecounter == 0) {
                if (ischeck()) {
                    parent.getAppletContext().showStatus("Black wins!");
                } else {
                    parent.getAppletContext().showStatus("Game is a draw!");
                }
            }
        }
    }

//prepare AI for a new game
    public void newgame() {
        if (parent != null) {
            parent.getAppletContext().showStatus("");
        }
        //kill AI thread
        if (th != null) {
            th.stop();
        }
        th = null;
        //gemerate original position
        int[] org = {99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 124, 22, 23, 25, 126, 23, 22, 124, 99, 99, 21, 21, 21, 21, 21, 21, 21, 21, 99, 99, 0, 0, 0, 0, 0, 0, 0, 0, 99, 99, 0, 0, 0, 0, 0, 0, 0, 0, 99, 99, 0, 0, 0, 0, 0, 0, 0, 0, 99, 99, 0, 0, 0, 0, 0, 0, 0, 0, 99, 99, 11, 11, 11, 11, 11, 11, 11, 11, 99, 99, 114, 12, 13, 15, 116, 13, 12, 114, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99};
        for (int i = 0; i < 120; i++) {
            board[i] = org[i];
            graphboard[i] = org[i];
        }
        repaint();
        movecounter = 0;
        color = 1;
        deep = 0;
        target = 1;
        genmove();
        code = 0;
    }
}
